﻿Ext.define("PSI.InPlan.TraceMainForm", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({ text: "运输动态", iconCls: "psi-button-overlays", handler: me.onTrace, scope: me });

        //result.push("-");
        //result.push({ text: "服务评价", iconCls: "psi-button-comments", handler: me.onComments, scope: me });

        result.push("-");
        result.push({ text: "运输完成", iconCls: "psi-button-ok", handler: me.onFinish, scope: me });
        result.push("-");
        result.push({ text: "刷新", iconCls: "psi-button-refresh", handler: me.onRefreshPlanGird, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "进口计划动态跟踪",
            tbar: me.createToolButtons(),
            items: [
                {
                    region: "center", xtype: "container", layout: "fit", border: false,
                    items: [me.getInPlanGrid()]
                }, {
                    region: "south", xtype: "container", border: false, height: 30,
                    items: [
                        {
                            xtype: "displayfield",
                            id: "editPlanInfo",
                            value: ""
                        }
                    ]
                }
            ]
        });

        me.panel = panel;

        me.refreshInPlanGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getInPlanGrid: function () {
        var me = this;

        if (!me.__inPlanGrid) {
            me.__inPlanGrid = me.createInPlanGrid();
        }

        return me.__inPlanGrid;
    },

    createInPlanGrid: function () {
        var modelName = "PSIInPlanModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "address", "cntNumber", "cntType", "deliverDT", "memo",
                "planDT", "dispatcher", "truckDT", "truckCompany", "truckNumber",
                "driver", "driverTel", "dt1Plan", "dt1Driver", "dt2Plan", "dt2Driver",
                "dt3Plan", "dt3Driver", "dt4Plan", "dt4Driver", "dt5Plan", "dt5Driver", "vsl", "clients", "cntOwner",
                "unloadMan", "unloadManTel", "traceMemo", "status"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "状态", dataIndex: "status", menuDisabled: true, width: 60 },
                    { header: "进口计划号", dataIndex: "planNumber", menuDisabled: true, width: 80 },
                    { header: "拖车公司", dataIndex: "truckCompany", menuDisabled: true },
                    { header: "车号", dataIndex: "truckNumber", menuDisabled: true },
                    { header: "司机", dataIndex: "driver", menuDisabled: true, width: 60 },
                    { header: "司机电话", dataIndex: "driverTel", menuDisabled: true },
                    { header: "卸货联系人", dataIndex: "unloadMan", menuDisabled: true, width: 70 },
                    { header: "卸货人电话", dataIndex: "unloadManTel", menuDisabled: true },
                    {
                        header: "提箱", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt1Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt1Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "出港", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt2Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt2Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "到卸货地", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt3Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt3Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "卸货开始", menuDisabled: true,
                        columns: [
                            { header: "调度卸货时间", dataIndex: "dt5Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt5Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "卸货完成", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt4Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt4Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "动态备注", dataIndex: "traceMemo", menuDisabled: true
                    },
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "送货时限", dataIndex: "deliverDT", menuDisabled: true, width: 120 },
                    { header: "委托备注", dataIndex: "memo", menuDisabled: true },
                    { header: "计划下达时间", dataIndex: "planDT", menuDisabled: true, width: 120 },
                    { header: "调度", dataIndex: "dispatcher", menuDisabled: true },
                    { header: "派车时间", dataIndex: "truckDT", menuDisabled: true, width: 120 }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    onRefreshPlanGird: function () {
        this.refreshInPlanGrid();
    },

    refreshInPlanGrid: function (selectedId) {
        var me = this;
        var grid = me.getInPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }

        var params = {
        };

        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetTraceListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                    var info = "进口计划共计: <span style='color:red'>" + data.length + "</span>个";
                    Ext.getCmp("editPlanInfo").setValue(info);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onTrace: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个进口计划");
            return;
        }
        var plan = items[0].getData();
        if (!plan.truckDT) {
            PSI.MsgBox.showInfo("当前计划还没有派车，无法录入动态信息");
            return;
        }

        var form = Ext.create("PSI.InPlan.TraceForm");
        form.show(me, plan);
    },

    onComments: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个进口计划");
            return;
        }

        var plan = items[0].getData();
        var form = Ext.create("PSI.InPlan.TraceComments");
        form.show(me, plan);
    },

    onFinish: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个进口计划");
            return;
        }

        var plan = items[0].getData();
        if (plan.status === "运输完成") {
            PSI.MsgBox.showInfo("当前计划已经完成");
            return;
        }

        PSI.MsgBox.confirm("请确认是否标记计划[" + plan.planNumber + "]运输完成?", function () {
            var el = Ext.getBody();
            el.mask("正在处理中...");
            Ext.Ajax.request({
                url: "/InPlan/Finish",
                method: "POST",
                params: { id: plan.id },
                callback: function (options, success, response) {
                    el.unmask();
                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功标记运输完成", function () {
                                me.refreshInPlanGrid();
                            });
                        } else {
                            PSI.MsgBox.showInfo(data.msg);
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误");
                    }
                }
            });
        });
    }
});